<?php
declare(strict_types = 1);

/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */
namespace App\Component;

use App\Constants\ChatCode;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Di\Annotation\Inject;

class RedisGroupSever
{

    public $redis;

    public function __construct() {
        $container = ApplicationContext::getContainer();
        $this->redis = $container->get(\Hyperf\Redis\Redis::class);
    }

    public function getKey($room)
    {
        return ChatCode::ROOM_GROUP . $room;
    }

    public function getUserGroupKey($fd)
    {
        return ChatCode::ROOM_USER_GROUP .":". $fd;
    }

    public function getUserKey($fd)
    {
        return ChatCode::USER_INFO .":". $fd;
    }

    public function getRoom($room)
    {
        return  $this->redis->hVals($this->getKey($room));
    }

    public function joinRoom($room, $fd)
    {
        $this->redis->hSet($this->getKey($room), "user" . $fd, $fd);
        $this->redis->hSet($this->getUserGroupKey($fd), "room" . $room, $room);
    }

    public function quitRoom($room, $fd)
    {
        $this->redis->hDel($this->getKey($room), "user" . $fd);
        $this->redis->hDel($this->getUserGroupKey($fd), "room" . $room);
    }

    public function getUserRoomList($fd)
    {
        return $this->redis->hVals($this->getUserGroupKey($fd));
    }

}

